﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using Job.Logic.Common;

namespace Job.Web.userControls.Public
{
    public partial class ContentDetailLeft : BaseCtrl
    {
        int subjectid = myR["subjectid"].IntValue;
        int categoryid = myR["categoryid"].IntValue;
        int contentid = myR["contentid"].IntValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();

                try 
                {
                    BaseInfo subjectInfo = WebCommon.GetSubjectInfo(subjectid);
                    this.lbSubjectName.Text = subjectInfo.DisplayTitle;
                }
                catch(Exception ex)
                {
                    this.lbSubjectName.Text = "";
                }

            }
        }


        void BindData()
        {
            DataSet ds = GetData(0);
            if (ds != null)
            {
                //DataRow[] drl= ds.Tables[0].Select("content_id=" + contentid);
                int index = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (Convert.ToInt32(ds.Tables[0].Rows[i]["content_id"]) == contentid)
                    {
                        index = i;
                        break;
                    }
                }
                
                while (ds.Tables[0].Rows.Count > index + 6)
                {
                    ds.Tables[0].Rows.RemoveAt(index + 6);
                }
                //ds.Tables[0].Rows.RemoveAt(index);
                while (ds.Tables[0].Rows.Count > 10)
                {
                    ds.Tables[0].Rows.RemoveAt(0);
                }

                

                this.dlList.DataSource = ds.Tables[0];
                this.dlList.DataBind();
            }
        }



        private DataSet GetData(int type)
        {
            ArrayList param = new ArrayList();

            if (subjectid > 0)
            {
                SqlParameter pm = new SqlParameter();
                pm.ParameterName = "subject_id";
                pm.Value = Constants.GetListIdByInfoId(subjectid);
                pm.SqlDbType = SqlDbType.Int;
                param.Add(pm);
            }

            SubjectLogic blogic = new SubjectLogic();
            int currentPageIndex = 1;
            if (Session["listcurrentPageIndex"] != null)
            {
                currentPageIndex = Convert.ToInt32(Session["listcurrentPageIndex"]);
            }
            int pagesize = 20;
            int realindex = 1;
            DataSet ds = new DataSet();
            if (currentPageIndex > 1)
            {
                //如果是奇数页，则每页15条，重新取数据
                if (currentPageIndex % 2 == 1)
                {
                    //如原来是第三页20-30条
                    //现在要取15-35条。分两次取15-30,30-45
                    pagesize = 15;
                    realindex = (currentPageIndex + 1) / 2;
                }
                else//如果是偶数页，则每页20条，重新取数据
                {
                    pagesize = 20;
                    realindex = currentPageIndex / 2;
                }
                ds = blogic.GetBaseContentList(pagesize, realindex, type, this.SortField, this.SortDirection, param);
                DataSet ds1 = blogic.GetBaseContentList(pagesize, realindex+1, type, this.SortField, this.SortDirection, param);
                if (ds1 != null && ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds1.Tables[0].Rows)
                    {
                        DataRow[] drl = ds.Tables[0].Select("content_id = " + dr["content_id"]);
                        if (drl == null || drl.Length < 1)
                        {
                            ds.Tables[0].ImportRow(dr);
                        }
                    }
                }
                return ds;
            }
            else
            {
                ds = blogic.GetBaseContentList(pagesize, realindex, type, this.SortField, this.SortDirection, param);
                return ds;
            }
            
        }

        /// <summary>
        /// 排序方式
        /// </summary>
        public Job.Web.Framework.SortDirection SortDirection
        {
            get
            {
                return Job.Web.Framework.SortDirection.Descending;
            }
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortField
        {
            get
            {
                return "order_no {0},content_id {1}";
            }
        }

        protected void dlList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink hlDetail = (HyperLink)e.Item.FindControl("hlDetail");
                        Label lblid = (Label)e.Item.FindControl("lblid");
                        //int _subjectid = Constants.GetInfoIdByListId(subjectid);
                        //设置具体subject的URL用来显示Detail信息。
                        //if (Convert.ToInt32(lblid.Text.Trim()) == contentid)
                        //{
                        //    hlDetail.CssClass = "detailSubjectSelected";
                        //}
                        hlDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid=" + categoryid + "&subjectid=" + subjectid + "&contentid=" + lblid.Text.Trim();
                        if (hlDetail.Text.Length > 15)
                        {
                            hlDetail.Text = hlDetail.Text.Substring(0, 14) + "...";
                        }
                        break;
                    }
            }
        }
    }
}